package com.sam.im.heiqun.uis.activities;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.iflytek.cloud.thirdparty.V;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.LiangSortItem;
import com.sam.im.heiqun.entities.MyGiftItem;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.LiangSortAdapter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.EventBusType;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/9/19.
 */

public class LiangSelecterSortActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    @BindView(R.id.ok)
    TextView ok;

    private PGService pgservice;
    private List<LiangSortItem.ListBean> datas = new ArrayList<>();
    private LiangSortAdapter mAdapter ;

    private String sessionid = "";
    private int type = 0 ;//0-是未注册直接购买；1-已注册，购买靓号

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.select_type);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        pgservice = PGService.getInstance();
        type = getIntent().getIntExtra("type", 0);
        ok.setText(getResources().getString(R.string.next));
        ok.setVisibility(View.VISIBLE);

        mAdapter = new LiangSortAdapter(LiangSelecterSortActivity.this, datas);
        recycler_view.setAdapter(mAdapter);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        inidata();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_liang_selecter_sort;
    }

    @OnClick({R.id.ok})
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.ok :

                LiangSortItem.ListBean liangSortItem = datas.get(mAdapter.getSelecterNum());

                LiangSelecterIMActivity.start(LiangSelecterSortActivity.this,sessionid,liangSortItem,type);

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onBaseEvent(final Integer refresh) {
        if (refresh == EventBusType.RESULT_PAY_LIANG_SUCCESS){
            finish();
        }
    }

    private void inidata() {
        showProgress("正在加载......");
        pgservice.getNumberType().subscribe(new AbsAPICallback<LiangSortItem>() {
            @Override
            protected void onResultError(ApiException ex) {
                showToast("请求数据失败！");
                hideProgress();
            }

            @Override
            public void onNext(final LiangSortItem myCircleItem) {
                if (myCircleItem != null) {
                    sessionid = myCircleItem.getSessid() ;
                    if (myCircleItem.getTypes().size() > 0) {
                        datas.clear();
                        datas.addAll(myCircleItem.getTypes());
                        mAdapter.notifyDataSetChanged();
                    }

                }
                hideProgress();
            }
        });
    }

}
